package com.firsteapps.mobile.android.puzzle;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.firsteapps.mobile.android.puzzle.game.GameScreen;
import com.firsteapps.mobile.android.puzzle.game.Helper;
import com.firsteapps.mobile.android.puzzle.game.PuzzleInfo;
import com.firsteapps.mobile.android.puzzle.util.Consts;
import com.firsteapps.mobile.android.puzzle.util.Utilities;

public class Continue extends PostLoginBase {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.continue_layout);

		TextView puzzleSizeViewer = (TextView) findViewById(R.id.continue_puzzle_size);
		TextView movesCountViewer = (TextView) findViewById(R.id.continue_puzzle_moves_count);
		TextView attemptsCountViewer = (TextView) findViewById(R.id.continue_puzzle_attempts_count);
		TextView timeLimitViewer = (TextView) findViewById(R.id.continue_puzzle_time_count);
		TextView tilesCountViewer = (TextView) findViewById(R.id.continue_puzzle_tiles_count);
		TextView picturePath = (TextView) findViewById(R.id.continue_picture_path);

		final String pausedPuzzle = getIntent().getStringExtra(Consts.PUZZLE_KEY);
		final PuzzleInfo info = Helper.loadCreatedPuzzle(this, pausedPuzzle);

		puzzleSizeViewer.setText(info.getSize() + ":" + info.getSize());
		movesCountViewer.setText(info.getMoveCount() + (info.getMaxMoves() > 0 ? "/" + info.getMaxMoves() : ""));
		attemptsCountViewer.setText(info.getAttempts() + (info.getMaxAttempts() > 0 ? "/" + info.getMaxAttempts() : ""));
		timeLimitViewer.setText(Helper.getElapsedTime(info.getElapsed())
				+ (info.getMaxTime() > 0 ? "/" + Helper.getElapsedTime(info.getMaxTime() * 1000) : ""));
		tilesCountViewer.setText(info.getSize() * info.getSize() + "");
		String pictureFileName = info.getFile();
		pictureFileName = pictureFileName.substring(pictureFileName.lastIndexOf("/") + 1);
		pictureFileName = pictureFileName.substring(0, pictureFileName.indexOf("."))+ ".jpg";
		picturePath.setText(pictureFileName);

		ImageView continueOld = (ImageView) findViewById(R.id.continue_continue);
		continueOld.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent myIntent = new Intent(Continue.this, GameScreen.class);
				myIntent.putExtra(Consts.PUZZLE_KEY, pausedPuzzle);
				// myIntent.putExtra(Consts.PUZZLE_CONTINUE_OR_NEW, true);
				startActivity(myIntent);
				finish();
				overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
			}
		});

		findViewById(R.id.continue_start_new).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Utilities.startActivityWithAnimation(Continue.this, SelectPuzzle.class);
			}
		});
		
		findViewById(R.id.autosolve_puzzle).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Helper.autosolvePuzzle(Continue.this, pausedPuzzle, info);
			}
		});
	}
}
